package api.actions;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.StringTokenizer;

import javax.swing.JFileChooser;

import api.Interface;

public class Abrir {
	public static File fin;

	public static void run() throws IOException {
		int value = Interface.FileChooser.showOpenDialog(Interface.frame);
		
		if (value == JFileChooser.APPROVE_OPTION) {
			fin = Interface.FileChooser.getSelectedFile();
			
			Interface.mnIni.setEnabled(false);
			Interface.mnEstrategiasaciegas.setEnabled(true);
			
			IniTablero(Interface.FileChooser.getSelectedFile().getAbsoluteFile());
			
			Interface.MostrarCasillas();
		}
	}
	
	private static void IniTablero(File fich) throws IOException {		
		BufferedReader br = new BufferedReader(new FileReader(fich));
		String linea = br.readLine();  //Cabecera fichero
		linea = br.readLine();    //linea en blanco
		linea = br.readLine();    //Dimensiones tablero
		linea = br.readLine();    //{n,m} 
		
		String aux = "";
		
		StringTokenizer st = new StringTokenizer(linea);
		aux= st.nextToken("{,}"); 
		int m = Integer.parseInt (aux);
		aux= st.nextToken("{,}");
		int n = Integer.parseInt (aux);
		
		
		Interface.getTablero().setN(n);
		Interface.getTablero().setM(m);

		linea = br.readLine();    //Posicion inicial:
		linea = br.readLine();    //{X,Y}
		
		StringTokenizer st2 = new StringTokenizer(linea);
		
		aux= st2.nextToken("{,}"); 
		int x = Integer.parseInt (aux);
		aux= st2.nextToken("{,}");
		
		int y = Integer.parseInt (aux);
		Point punto = new Point(x,y);
		
		Interface.getTablero().getRobot().setPosInicial(punto);
		Interface.getTablero().getRobot().setPosActual(punto);
		
		linea = br.readLine();    //Posicion Final
		linea = br.readLine();    //{x,y}
		
		StringTokenizer st3 = new StringTokenizer(linea);
		aux= st3.nextToken("{,}"); 
		x = Integer.parseInt (aux);
		aux= st3.nextToken("{,}"); 
		y = Integer.parseInt (aux);
		punto = new Point(x,y);
		Interface.getTablero().getRobot().setPosFinal(punto);
		
		
		linea = br.readLine();    //OBSTACULOS
		linea = br.readLine();    //Numero de obstaculos
		linea = br.readLine();    //n
		n = Integer.parseInt (linea);
		
		HashSet<Point> s = new HashSet<Point>();

		linea = br.readLine();    //Posiciones.
		for (int i = 0; i < n; i++) {
			
			linea = br.readLine();    //{x,y}

			StringTokenizer st4 = new StringTokenizer(linea);
			aux= st4.nextToken("{,}"); 
			x = Integer.parseInt (aux);
			aux= st4.nextToken("{,}"); 
			y = Integer.parseInt (aux);
			
			punto = new Point(x,y);

			s.add(punto);
		}
		
		//System.out.println("Dimensiones: " + Interface.getTablero().getM() + " " + Interface.getTablero().getN());
		//System.out.println("Posicion Inicial: " + Interface.getTablero().getRobot().getPosInicial());
		//System.out.println("Posicion Final: " + Interface.getTablero().getRobot().getPosFinal());
		Interface.getTablero().SetObs(s);
	}
}
